$(document).ready(function () {
	i=getParameterByName("id")-1

  console.log(i);
    sankofaVolunteerinfo.loadVolunteerinfo(i);


    sankofaEventVolunteerinfo.loadEventVolunteerinfo();
    

});

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

sankofaVolunteerinfo = {
     
    // Set twitter username, number of tweets & id/class to append tweets

    // core function of jqtweet
    // Add volunteer to table under volunteer edit
    addVolunteersinfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/volunteers',
            type: 'POST',
            dataType: 'json',

            data:{
               

                vlastname: $('#vlastname'+i).val(),
                vfirstname: $('#vfirstname'+i).val(),
                vprefix: $('#vprefix'+i).val(),
                vsuffix: $('#vsuffix'+i).val(),
                vage: $('#vage'+i).val(),
                vemail: $('$vemail'+i).val(),
                vgender: $('$vgender'+i).val(),
                vrace: $('$vrace'+i).val(),
                vwebsite: $('$vwebsite'+i).val(),
            },
            //Post into squlite3
            success: function(data, textStatus, xhr) {
                Console.log('Load was performed.')
                
            //   window.location = "addvolunteer.html";
            //   var html =  '<div class="tweet">TWEET_TEXT<div class="time">AGO</div>';
            //   $(sankofaVolunteer.appendTo).append( html.replace('TWEET_TEXT', data[i].text)
            // .replace('AGO', data[i].created_at)
              
            }
        });
    },

            
//       $(JQTWEET.appendTo).append("<div>SUCCESS</div>" + data.length);    

               
         
      //            // append tweets into page
      //            for (var i = 0; i < data.length; i++) {
      //               $(sankofaVolunteer.appendTo).append(
      //                   html.replace('TWEET_TEXT', data[i].text)
      // .replace('AGO', data[i].created_at)
      //               );
            //      }                  

            // }   

         // });
      

    //Load Volunteers onto the page
    loadVolunteerinfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/volunteers',
            type: 'GET',
            dataType: 'json',

            // i is goin got be the number of the volunteer
            

            success: function(data, textStatus, xhr)
            
              {
                //function addTableRow(table)
                
                // clone the last row in the table
                 //var $tr = $('#volunteernametable').find("tbody tr:last").clone();
                 //$tr.find("input").attr("value","");
             
                // get the name attribute for the input and select fields
                //var dataa = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'];
                
                    //make the td

                    var isNull = function(argument) {
                    if(argument === null || argument == ""){
                      return "N.A.";
                    }
                      console.log("isNull called");
                      return argument;

                }

                    $("#name").html(isNull(data[i].vfirstname)+" "+isNull(data[i].vlastname))

                 // $("#basicinfo").find("#vname").html(isNull(data[i].vfirstname)+" "+isNull(data[i].vlastname))
                 // $("#basicinfo").find("#vage").addClass("volunteers_row-3").html(isNull(data[i].vage))
                 // // // $("#volunteerinfotable").find("svalue").addClass("volunteers_row-4").html(data[i].vsuffix)
                 // // $("#volunteerinfotable").find("#evalue").addClass("volunteers_row-5").html(data[i].vemail)
                 // $("#basicinfo").find("#vgender").addClass("volunteers_row-6").html(isNull(data[i].vgender))
                 // $("#basicinfo").find("#vrace").addClass("volunteers_row-7").html(isNull(data[i].vrace))
                 // // $("#basicinfo").find("#addressvalue").addClass("volunteers_row-8").html(data[i].vaddress)
                 // // // $("#volunteerinfotable").find("dvalue").addClass("volunteers_row-9").html(data[i].v)
                 // // $("#volunteerinfotable").find("#wvalue").addClass("volunteers_row-10").html(data[i].vwebsite)
                 // $("#basicinfo").find("#vhours").html("test")

                 $("#infoinsert").find("#vname1").html(isNull(data[i].vfirstname)+" "+isNull(data[i].vlastname))
                 $("#infoinsert").find("#vage1").html(isNull(data[i].vage))
                 $("#infoinsert").find("#vemail1").html(isNull(data[i].vemail))
                 $("#infoinsert").find("#vgender1").html(isNull(data[i].vgender))
                 $("#infoinsert").find("#vrace1").html(isNull(data[i].vrace))
                 $("#infoinsert").find("#vaddress1").html(isNull(data[i].vaddress))
                 $("#infoinsert").find("#vphonenumber1").html(isNull(data[i].vphonenumber))
                 $("#infoinsert").find("#vwebsite1").html(isNull(data[i].vwebsite))









               }                  
            });           
                 //console.log("LOADED");
            }  
            
            
            
            
            
            
            }   
    
sankofaEventVolunteerinfo = {
     
    // Set twitter username, number of tweets & id/class to append tweets

    // core function of jqtweet
    // Add volunteer to table under volunteer edit
    addEventVolunteersinfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/EventVolunteer',
            type: 'POST',
            dataType: 'json',

            data:{
               

                vlastname: $('#vlastname'+i).val(),
                vfirstname: $('#vfirstname'+i).val(),
                vprefix: $('#vprefix'+i).val(),
                vsuffix: $('#vsuffix'+i).val(),
                vage: $('#vage'+i).val(),
                vemail: $('$vemail'+i).val(),
                vgender: $('$vgender'+i).val(),
                vrace: $('$vrace'+i).val(),
                vwebsite: $('$vwebsite'+i).val(),
            },
            //Post into squlite3
            success: function(data, textStatus, xhr) {
                Console.log('Load was performed.')
                
            //   window.location = "addvolunteer.html";
            //   var html =  '<div class="tweet">TWEET_TEXT<div class="time">AGO</div>';
            //   $(sankofaVolunteer.appendTo).append( html.replace('TWEET_TEXT', data[i].text)
            // .replace('AGO', data[i].created_at)
              
            }
        });
    },

            
//       $(JQTWEET.appendTo).append("<div>SUCCESS</div>" + data.length);    

               
         
      //            // append tweets into page
      //            for (var i = 0; i < data.length; i++) {
      //               $(sankofaVolunteer.appendTo).append(
      //                   html.replace('TWEET_TEXT', data[i].text)
      // .replace('AGO', data[i].created_at)
      //               );
            //      }                  

            // }   

         // });
      

    //Load events onto the page
    loadEventVolunteerinfo: function() {
        $.ajax({
            url: 'http://localhost:4567/events',
            type: 'GET',
            dataType: 'json',

            

            success: function(data, textStatus, xhr)
            
              {
                //function addTableRow(table)
                
                // clone the last row in the table
                 //var $tr = $('#volunteernametable').find("tbody tr:last").clone();
                 //$tr.find("input").attr("value","");
             
                // get the name attribute for the input and select fields
                //var dataa = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'];
                
                for (i = 0; i < data.length; i++){
                    //make the td
                var $td = $('<td></td>').addClass("events_col-1").attr('id', 'ename'+i)
                var $tda = $('<td></td>').addClass("events_col-2").attr('id', 'ebegindate'+i)
                var $tdb = $('<td></td>').addClass("events_col-3").attr('id', 'eenddate'+i)
                var $tdc = $('<td></td>').addClass("events_col-4").attr('id', 'edescription'+i)


                    // make a tr and add td to tr
                var $tr = $('<tr></tr>').append($td,$tda,$tdb,$tdc).addClass("a_event_row"+i).attr("id","a_event_row"+i).attr("onclick","takemetoeventinfo("+i+")")

                //$tr.replace("last_name"+i,"data[i]");
                //     )
                // adding to volunteer search table
              

               //  .addClass("a_volunteer")
               //  $('.volunteers').html(data[0].vlastname);
               //  $('#firstfirstname').html(data[0].vfirstname);
               //  $('#firstage').html(data[0].vage);

               //  $('#secondlastname').html(data[1].vlastname);
               //  $('#secondfirstname').html(data[1].vfirstname);
               //  $('#secondage').html(data[1].vage);
               $('#eventVolunteerTable').append($tr)
               $('#eventVolunteerTable').find("#ename"+i).html(data[i].ename);
               $('#eventVolunteerTable').find("#ebegindate"+i).html(data[i].ebegindate);
               $('#eventVolunteerTable').find("#eenddate"+i).html(data[i].eenddate);
               $('#eventVolunteerTable').find("#edescription"+i).html(data[i].edescription);




    
            }
               
                    
                }
            });


            
                 //console.log("LOADED");
            }   

            }   